//
//  ViewController.m
//  05—掌握-利用系统app导航_3d视角_地图快照截图
//
//  Created by J on 16/2/28.
//  Copyright © 2016年 J. All rights reserved.
//

#import "ViewController.h"
#import <MapKit/MapKit.h>
@interface ViewController ()
@property(nonatomic,strong)CLGeocoder* geoCoder;
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.geoCoder = [CLGeocoder new];
}


////3d视角
//- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//
//    self.mapView.camera = [MKMapCamera cameraLookingAtCenterCoordinate:CLLocationCoordinate2DMake(22.273093, 114.178166) fromEyeCoordinate:CLLocationCoordinate2DMake(22.283093, 114.178166) eyeAltitude:10];
//
//    
//    
////地图快照截图
//    MKMapSnapshotOptions* option=[MKMapSnapshotOptions new];
//    //针对地图的设置
//    option.region = self.mapView.region;//当前显示的范围
//    option.showsBuildings = YES;
////    输出图片设置
//    option.size = self.view.bounds.size;
//    MKMapSnapshotter* shoptter = [[MKMapSnapshotter alloc] initWithOptions:option];
//    
//    [shoptter startWithCompletionHandler:^(MKMapSnapshot * _Nullable snapshot, NSError * _Nullable error) {
//        if (error) {
//            NSLog(@"截图失败");
//            return ;
//        }
//        NSLog(@"%@",snapshot.image);
//      NSData* data =  UIImagePNGRepresentation(snapshot.image);
//        [data writeToFile:@"/Users/J/Desktop/截图.png" atomically:YES];//原子性 要不全部写入 要不失败
//        UIImageView* imageView = [[UIImageView alloc]initWithImage:snapshot.image];
//        [self.view addSubview:imageView];
//        
//    }];
//    
//    
//
//}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.geoCoder geocodeAddressString:@"广州" completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        
        if (!error) {
            CLPlacemark* fistPlace = [placemarks firstObject];
            
            [self.geoCoder geocodeAddressString:@"上海" completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
                
                if (!error) {
                    CLPlacemark* secondPlace = [placemarks firstObject];
                    [self beginNaviWithBeging:fistPlace andEndP:secondPlace];
                }
            }];
        }
    }];
    
    
}



- (void)beginNaviWithBeging:(CLPlacemark*)beginP andEndP:(CLPlacemark*)endPlaceMark{
    
//     To create an MKPlacemark from a CLPlacemark, call [MKPlacemark initWithPlacemark:] passing the CLPlacemark instance that is returned by CLGeocoder.
//     See CLGeocoder.h and CLPlacemark.h in CoreLocation for more information.
//    通过CLGeocoder 返回 CLPlacemark

    
    CLPlacemark* clPbegin = beginP;
    MKPlacemark* cl = [[MKPlacemark alloc]initWithPlacemark:clPbegin];
    MKMapItem* beginItem = [[MKMapItem alloc] initWithPlacemark:cl];
        
    CLPlacemark* clPEnd = endPlaceMark;
    MKPlacemark* clEnd = [[MKPlacemark alloc]initWithPlacemark:clPEnd];
    MKMapItem* endItem = [[MKMapItem alloc] initWithPlacemark:clEnd];
    NSArray* items = @[beginItem,endItem];//地图项数组
    NSDictionary* dic =@{//启动字典
                         MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,//导航方式
                         MKLaunchOptionsMapTypeKey:@(MKMapTypeSatelliteFlyover),//地图类型
                         MKLaunchOptionsShowsTrafficKey:@(YES)};//是否显示交通
    [MKMapItem openMapsWithItems:items launchOptions:dic];

}


@end
